home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Util / Md-Mz / Momentum / Momentum.a < prev    next >
Encoding:
Text File  |  1992-06-09  |  12.8 KB  |  330 lines  |  [TEXT/MPS ]

  1.     ;
  2.     ;                                    M O        -         T h e   M o m e n t u m   C D E V
  3.     ;
  4.     ;    INIT to patch DragGrayRgn to allow ‘tossing’ of windows
  5.     ;    © 1991, 1992  Exodus Software
  6.     ;    Programmed by Jim Stegman & Bill Johnson.
  7.     ;    Idea by David Griggs, Apple System Engineer @ Cincinnati Office
  8.     
  9.     ;    The idea is to allow a user to drag a window, then release the mouse button
  10.     ;    while still dragging, to ‘toss’ it across the screen.
  11.     
  12.     ;    DragGrayRgn is called whenever a programmer wants the user to see feedback of 
  13.     ;    a dragging operation, by dragging around a gray outline of the item. DragGrayRgn 
  14.     ;    maintains control until the mouse button is released.
  15.     
  16.     ;    We need to alter it’s functionality in two ways: we need control once the user has
  17.     ;    released the mouse, so we can calculate the velocity and direction to ‘toss’ the region,
  18.     ;    and then to actually move it across the screen along its trajectory. But in order to
  19.     ;    be able to calculate the velocity and direction, we need to know where the region
  20.     ;    was before the user released the button, as well as after. Once the region comes
  21.     ;    to a “rest”, where it’s velocity is zero, we return control to the caller.
  22.     
  23.     ;    DragGrayRgn has an ActionProc parameter, which is a routine supplied by the
  24.     ;    programmer, that will be called repeatedly while the user is dragging. This INIT
  25.     ;    patches DragGrayRgn so that our ActionProc is installed. This is used here to
  26.     ;    write the latest mouseLoc and TickCount into our data area. Care is taken if there
  27.     ;    is already an ActionProc, so that it gets passed execution after our ActionProc.
  28.     
  29.     ;    The original DragGrayRgn is called by JSR so that we get execution back when it is
  30.     ;    done, so that we can ‘toss’ the region.
  31.         
  32.     ;    This INIT has a number of variables which are adjustable by the CDEV. They
  33.     ;    are as follows:
  34.     
  35.             ;    Force of Gravity:    an integer from 0 to 100 that will pull the region
  36.             ;                                in a given direction. Zero is NO gravity, 100 is max.
  37.             ;    Direction of Gravity: a point that defines the ‘head’ of an arrow, where 
  38.             ;                                the tail is at (0, 0). Thus, (1, 0) for a direction is 
  39.             ;                                equivalent to a gravity pull of straight down.
  40.             ;    DeskTop Friction:    an integer from 1 to 100 that represents at what rate
  41.             ;                                the region will slow down. One is very low friction, 
  42.             ;                                and a value of 100 will make it operate normally-no toss
  43.             ;    Bounce Friction:    an integer from 1 to 100 that represents how much ‘energy’
  44.             ;                                is lost each time the region ‘bounces’ off one of the sides
  45.             ;                                of the screen. A value of 100 will make the region ‘stick’
  46.             ;                                to the side, unless a high force of gravity pulls it down.
  47.             
  48.             ;    All parameters that are listed from 0 or 1 to 100 are constrained to that
  49.             ;    range. In other words, 100 is used if a value of 23,719 is entered.
  50.  
  51.     IMPORT    InstallGestaltPtrReference
  52.     IMPORT    TossGrayRgn
  53.     IMPORT    IsMoEnabled
  54.     IMPORT    ShowINIT                    ; include the routine that displays the icon at boot time
  55.  
  56.  
  57.     PRINT            OFF
  58.         INCLUDE 'QuickEqu.a'
  59.         INCLUDE 'ScriptEqu.a'
  60.         INCLUDE 'ToolEqu.a'
  61.         INCLUDE 'SysEqu.a'
  62.         INCLUDE 'Traps.a'
  63.         INCLUDE 'PackMacs.a'
  64.     PRINT            ON
  65.  
  66.  
  67. lagTime            equ                2        ; min. num of ticks inbetween time/mouseLoc samplings
  68. patchProcPtr    equ              -4        ; our local VAR for Install routine
  69. DragTrap            equ          $105        ; trap number for DragGrayRgn ($A905)
  70. PatchSize            equ          6000        ; number of bytes allocated for the patches in RAM - can't do it
  71.                                                     ; by subtracting addresses because we need to include linked-in 
  72.                                                     ; Pascal.
  73. RsrcSize            equ              14
  74. IconID                EQU           -4064        ; the resource id of our ICN#
  75.  
  76.  
  77. STARTPLACE        FUNC    EXPORT
  78.  
  79.         ; here is the entry, where we are called at boot time
  80.  
  81.     MOVE.W    #IconID, -(SP)            ; push the icon id
  82.     MOVE.W    #-1, -(SP)                ; default parameter
  83.     BSR            ShowINIT                    ; draw the icon
  84.         
  85.     CLR.W        -(SP)            ; make room for a result
  86.     _Button                        ; is the mouse button down?
  87.     MOVE.W    (SP)+, D0        ; herein lies the answer
  88.     BNE.S        WeBeeDone    ; if so, do not install me
  89.     
  90.     BRA.S        Install            ; patch as patch can!
  91. WeBeeDone
  92.     RTS
  93.  
  94. ;********   I N S T A L L  I N I T   ************
  95.         ;    The Install routine below puts all the code and data between the labels,
  96.         ;    START & FINISH, into the System Heap.
  97.     
  98. Install
  99. ;    _debugger
  100.         ; get and save the addresses of the original routines
  101.     MOVE.W    #DragTrap, D0                        ; GetTrapAddress expects the trap num in D0
  102.     _GetTrapAddress                                    ; … and returns the trap address in A0
  103.     LEA            oldDragTrap, A1
  104.     MOVE.L        A0, (A1)                                ; save the old trap address
  105.     
  106.         ; allocate memory in the System Heap for our code
  107.         
  108.     MOVE.L        #PatchSize, D0                        ; NewPtr expects the SIZE to be in D0
  109.     _NewPtr    SYS                                        ; … and returns the new pointer in A0
  110.     TST            D0                                        ; check the result code
  111.     BNE.S        WeBeeDone                            ; if it’s an error, DO NOT install!
  112.     
  113.     LINK            A6, #-4                                ;    we have 1 local: patchProcPtr
  114.     MOVE.L        A0, patchProcPtr(A6)            ; save the address of our new ptr
  115.     
  116.     CLR.L        -(SP)                                    ; make room for the resource handle
  117.     MOVE.L        #$4D4F6474, -(SP)                ; type 'MOdt'
  118.     MOVE.W    #$F038, -(SP)                        ; with ID# -4040
  119.     _GetResource
  120.     MOVE.L    (SP)+, D0                                    ; get the resource handle
  121.     BEQ.S        NoResource                            ; if nil, use our defaults that exist in the DC statements
  122.     MOVEA.L    D0, A0
  123.     MOVEA.L    (A0), A0                                ; dereference it to get the source address
  124.     LEA            TossParam, A1                        ; get the destination
  125.     MOVE.L        #RsrcSize, D0
  126.     _BlockMove                                            ; copy it into this code
  127.     
  128. NoResource
  129.     MOVEA.L    patchProcPtr(A6), A1            ; get ready for our BlockMove
  130.     LEA            Start, A0                                ; set up the source address
  131.     MOVE.L        #PatchSize, D0                        ; BlockMove expects the SIZE to be in D0
  132.     _BlockMove                                            ; install our code & data in the System Heap
  133.         
  134.     ;    the following allows the CDEV to change our parameters
  135.     LEA            TossParam, A0
  136.     LEA            Start, A1
  137.     SUBA.L        A1, A0                                    ; determine the offset
  138.     MOVE.L        A0, D0
  139.     MOVEA.L    patchProcPtr(A6), A1            ; get our ptr in the system heap
  140.     ADDA.L        D0, A1                                    ; make it point to the TossParam data structure
  141.     MOVE.L        A1, -(SP)                                ; push the ptr
  142.     JSR            InstallGestaltPtrReference    ; install it so the CDEV can reference it
  143.     
  144.         ; patch the DragGrayRgn trap
  145.         
  146.     MOVE.W    #DragTrap, D0                        ; SetTrapAddress expects the trap num in D0
  147.     MOVE.L        patchProcPtr(A6), A0            ; also the address of the new routine in A0
  148.     _SetTrapAddress                                    ; the new routine is installed
  149.     
  150.  
  151.     UNLK        A6
  152.     RTS                                    ; installation is complete!
  153.     
  154.         ;    next is the stuff that will get called whenever the trap DragGrayRgn is
  155.         ;    invoked. First, we install our ActionProc, then call DragGrayRgn. When
  156.         ;    DragGrayRgn returns, we will commence tossing.
  157.         ;        
  158.         ;    State of the Stack when called: ( after I execute my LINK A6 instruction )
  159.         ;
  160.         ;            DESC            SIZE            OFFSET(A6)        PASCAL TYPE
  161.         ;        result space          4                    30                longInt
  162.         ;        theRgn                  4                    26                RgnHandle
  163.         ;        startPt                  4                    22                point
  164.         ;        limitRect              4                    18                Rect (RectPtr since Rect > 4 bytes)
  165.         ;        slopRect              4                    14                Rect (RectPtr since Rect > 4 bytes)
  166.         ;        axis                      2                    10                integer
  167.         ;        ActionProc          4                     8                    ProcPtr
  168.         ;        return address      4                     4                    pointer
  169.         ;                A6              4                     0
  170.  
  171. Start
  172. ;********   D R A G G R A Y R G N  P A T C H   ************
  173.  
  174.     CLR.W        -(SP)                            ; clear room on the stack for the boolean result
  175.     PEA            TossParam                    ; the address of the TossParam Block
  176.     JSR            IsMoEnabled
  177.     MOVE.W    (SP)+, D0                        ; get the result
  178.     BNE.S        MoIsGo
  179.     ; Mo is not enabled, just call the original DragGrayRgn
  180.     LEA            oldDragTrap, A0            ; get the original address of the call
  181.     MOVEA.L    (A0), A0                        ; dereference to get the trap address
  182.     JMP            (A0)                                ; execute it
  183.     
  184. MoIsGo
  185.     LINK            A6, #0                            ; no locals req’d, but a stack frame helps
  186.     LEA            oldMouseLoc, A0
  187.     MOVE.L        22(A6), (A0)                ; init our mouse location to the startPoint
  188.     LEA            oldTime, A0
  189.     MOVE.L        Ticks, (A0)                    ; init to the current time
  190.     LEA            oldActionProc, A0
  191.     MOVE.L        8(A6), (A0)                    ; get the passed in ActionProc, & save it
  192.     
  193.                                                         ; now get ready to call DragGrayRgn
  194.     CLR.L        -(SP)                            ; make room for our result
  195.     MOVE.L        26(A6), -(SP)                ; push the theRgn parmeter
  196.     MOVE.L        22(A6), -(SP)                ; push the startPt
  197.     MOVE.L        18(A6), -(SP)                ; push the address of the limitRect
  198.     MOVE.L        14(A6), -(SP)                ; push the address of the slopRect
  199.     MOVE.W    10(A6), -(SP)                ; push the axis parameter
  200.     PEA            myActionProc                ; push the address of my Action proc
  201.     LEA            oldDragTrap, A0            ; get the original address of the call
  202.     MOVEA.L    (A0), A0                        ; dereference to get the trap address
  203.     JSR            (A0)                                ; execute it
  204.  
  205.                 ; execution comes back here when the mouse is released
  206.     MOVE.L        (SP)+, D2                        ; get the result
  207.     CMP.W        #$8000, D2                    ; was it out of the slop Rect?
  208.     BEQ.S        QuitDrag                        ; yes, don’t toss it
  209.  
  210. ;    JSR            UpdateOldLocAndTime    ; get the latest & greatest mouseLoc & time
  211.     CLR.L        -(SP)                            ; make room for our result
  212.     MOVE.L        26(A6), -(SP)                ; push the theRgn parmeter
  213.     MOVE.L        D2, -(SP)                        ; push the result from DragGrayRgn
  214.     MOVE.L        22(A6), -(SP)                ; push the startPt
  215.     MOVE.L        18(A6), -(SP)                ; push the address of the limitRect
  216.     MOVE.L        14(A6), -(SP)                ; push the address of the slopRect
  217.     MOVE.W    10(A6), -(SP)                ; push the axis parameter
  218.     PEA            ScratchParam                ; the address of the ScratchParam Block
  219.     PEA            TossParam                    ; the address of the TossParam Block
  220.     JSR            TossGrayRgn                ; toss the window until it stops
  221.     MOVE.L        (SP)+, D2                        ; get the result
  222. QuitDrag
  223.     UNLK        A6                                ; dump our local stack frame
  224.     MOVEA.L    (SP)+, A0                        ; get our caller
  225.     ADDA.L        #22, SP                        ; dump the parameters
  226.     MOVE.L        D2, (SP)                        ; set the result
  227.     JMP            (A0)                                ; return to whomever called me…
  228.     
  229.  
  230. ;********   A C T I O N  P R O C   ************
  231. myActionProc
  232.     JSR            SetLocAndTime
  233.     LEA            oldActionProc, A0        ; get the original proc
  234.     MOVEA.L    (A0), A0
  235.     MOVE.L        A0, D0                            ; move it to a data register so we can check its validity
  236.     BLE.S        nilActionProc                ; is it nil?
  237.     JMP            (A0)                                ; no, jump to it
  238. nilActionProc
  239.     RTS                                                ; yes, it’s nil, just return
  240.     
  241. ;********   S U B R O U T I N E S   ************
  242. ArchiveOldLocAndTime        ; move oldMouseLoc & oldTime into archiveMouse & archiveTime
  243.     LEA            oldTime, A1
  244.     LEA            archiveTime, A0
  245.     MOVE.L        (A1), (A0)
  246.     LEA            oldMouseLoc, A1
  247.     LEA            archiveMouse, A0
  248.     MOVE.L        (A1), (A0)
  249.     RTS
  250.     
  251. UpdateOldLocAndTime                        ; get the latest mouseLoc & time
  252.     LEA            oldMouseLoc, A1
  253.     MOVE.L        A1, -(SP)                        ; push the VAR mouseLoc
  254.     _GetMouse                                    ; find the current location
  255.     LEA            oldTime, A0
  256.     MOVE.L        Ticks, (A0)                    ; update to the current time
  257.     RTS
  258.     
  259. SetLocAndTime
  260.     MOVE.L        Ticks, D0                        ; what time is it?
  261.     LEA            oldTime, A1
  262.     SUB.L        (A1), D0                        ; how long has it been since we last checked?
  263.     CMPI.L        #lagTime, D0                ; see if enough time has elapsed
  264.     BLT.S        tooSoon                        ; not yet, maybe next time
  265.     JSR            ArchiveOldLocAndTime
  266.     JSR            UpdateOldLocAndTime
  267. tooSoon
  268.     RTS
  269.     
  270. CheckCurrApp
  271.     MOVEQ        #0, D0                            ; init our result to false
  272.     MOVE.L        CurApName, D1            ; get the first 4 bytes of the application
  273.     CMP.L        FinderName, D1            ; compare the first 4 bytes (including length byte)
  274.     BNE.S        notFinder
  275.     MOVE.W    CurApName+4, D1        ; get the next 2 bytes
  276.     CMP.W        FinderName+4, D1        ; compare the next 2 bytes
  277.     BNE.S        notFinder
  278.     MOVE.B    CurApName+6, D1            ; get the last byte
  279.     CMP.B        FinderName+6, D1        ; compare the last byte
  280.     BNE.S        notFinder
  281.     MOVEQ        #1, D0                            ; it’s true, the current application is the Finder
  282. notFinder
  283.     RTS
  284.     
  285. ScratchParam
  286. oldDragTrap                DC.L    $FFFFFFFF    ; original DragGrayRgn procedure ptr stored here
  287. oldActionProc            DC.L    $FFFFFFFF    ; the ActionProc of my DragGrayRgn caller is kept here
  288. oldMouseLoc                DC.L    $FFFFFFFF    ; a mouse Loc point is stored here
  289. archiveMouse            DC.L    $FFFFFFFF    ; a mouse Loc point is stored here
  290. oldTime                    DC.L    $FFFFFFFF    ; a value of time ( Ticks ) is stored here
  291. archiveTime                DC.L    $FFFFFFFF    ; a value of time ( Ticks ) is stored here
  292. myKeyMap                DC.L    $00000000    ; a KeyMap is stored here
  293.                                 DC.L    $00000000    ;     a KeyMap is a packed array[0..127] of boolean
  294.                                 DC.L    $00000000    ;     which equals 16 bytes
  295.                                 DC.L    $00000000
  296. theWorld                    DC.L    $00000000    ; a rect that is the bounds of the screen
  297.                                 DC.L    $00000000
  298. ZeroPotential            DC.W $0002
  299.  
  300. TossParam
  301. DeskFric                    DC.W $0001            ; the Desk Friction value is stored here
  302. BounceFric                DC.W $0008            ; the Bounce Friction value is stored here
  303. GravForce                DC.W $0010            ; the Gravitational Force is stored here
  304. ClockDir                    DC.W $0006
  305. GravVect                    DC.L    $00400000    ; Gravitational v & h Vectors
  306. MoIsRunning                DC.B $01
  307. OnlyInFinder                DC.B $00
  308. MakeSound                DC.W $FFFF            ; should we make a sound when it bounces?
  309. SndResNum                DC.W $8100            ; this is a nice short blip sound in the System
  310. SndChannel                DC.L $00000000
  311. yVelocity                    DC.L $10002000    ; region velocity v vector
  312. xVelocity                    DC.L $30004000    ; region velocity h vector
  313. RegionLoc                    DC.L    $00000000    ; region location point
  314. ElapsedTicks                DC.L    $00000000    ; time since last moved ( Ticks )
  315. LongPositionH            DC.L    $00000000
  316. LongPositionV            DC.L    $00000000
  317. NoMoveCycles            DC.W $0000
  318. LastSoundTime            DC.L    $00000000
  319. MedGraySmoke            DC.L    $92244992
  320.                                 DC.L    $24499224
  321. LtGraySmoke            DC.L    $88221144
  322.                                 DC.L    $88221144
  323. Finish
  324.  
  325.  
  326.     ENDFUNC
  327.  
  328.  
  329.     END
  330.